<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 

class JL_Team extends JLClass{
  
  var $id;
  var $name;
  var $middle_name;
  var $short_name;
  var $description;
  var $club_id;
  var $checked_out;
  var $checked_out_time;
  
  function JL_Team( &$db ) {
		$this->mosDBTable( '#__joomleague_teams', 'id', $db );
	}
  
  function assignPlayers($p, $post = null){
    
    if(isset($post["players"])){
      
      $query = sprintf("SELECT p.id 
                FROM #__joomleague_players p, #__joomleague_playertool pt
               WHERE p.id = pt.player_id AND pt.project_id = %s AND pt.team_id = %s",$p->id, $this->id);
              
      $this->_db->setQuery($query);
      
      $arr1 = $this->_db->loadResultArray(); //old
      $arr2 = explode(",",$post["players"]); //new
      
      //print_r($arr1);
      //print "<br/>";
      //print_r($arr2);
      
      $diff1 = array_diff($arr1,$arr2); // remove these Items
      $diff2 = array_diff($arr2,$arr1); // insert these Items
      
      #remove
      if(count($diff1) > 0){
                
        $list = sprintf("(%s)", implode(",",$diff1));
        $this->_db->setQuery(sprintf("DELETE FROM #__joomleague_playertool WHERE project_id = %s AND team_id = %s AND player_id in %s" , $p->id, $this->id, $list));
        $this->_db->query();
        print $this->_db->getQuery();
      }
      #insert
      if(count($diff2) > 0){
        $query = "";
        foreach($diff2 as $v){
            $query .= sprintf("insert into #__joomleague_playertool (project_id,team_id,player_id) values (%s,%s,%s);\n",$p->id, $this->id, $v);
        }
        
        $this->_db->setQuery($query);
        print $this->_db->getQuery();
        $this->_db->query_batch(false);
        
        //print mysql_error(); 
      } 
    }
  }
  
}

?>
